#!/usr/bin/env perl

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

use strict;
use warnings;

use File::Path qw(make_path);
use IO::File;
use Avro::DataFile;
use Avro::DataFileWriter;

my $schema = do {
    my $fh = IO::File->new('../../share/test/schemas/interop.avsc');
    local $/ = undef;
    <$fh>
};

my $datum = {
    intField    => 12,
    longField   => 15234324,
    stringField => 'hey',
    boolField   => 1,
    floatField  => 1234.0,
    doubleField => -1234.0,
    bytesField  => '12312adf',
    nullField   => undef,
    arrayField  => [5.0, 0.0, 12.0],
    mapField    => {
        a   => { label => 'a'   },
        bee => { label => 'cee' },
    },
    unionField  => 12.0,
    enumField   => 'C',
    fixedField  => '1019181716151413',
    recordField => {
        label    => 'blah',
        children => [ { label => 'inner', children => [] } ]
    },
};

while (my ($codec, $enabled) = each(%Avro::DataFile::ValidCodec)) {
    next unless $enabled;
    my $outdir = '../../build/interop/data';
    make_path($outdir);
    my $path = sprintf('>%s/perl%s.avro', $outdir,
                       $codec eq 'null' ? '' : '_'.$codec);
    my $fh = IO::File->new($path);
    my $writer_schema = Avro::Schema->parse($schema);
    my $writer = Avro::DataFileWriter->new(
        fh => $fh,
        codec => $codec,
        writer_schema => $writer_schema
    );
    $writer->print($datum);
    $writer->close;
}
